<?php

use \Mapper\MemberModel as MemberMapper;
use \Mapper\GameCircleModel as GameCircleMapper;

class InformController extends \Base\AbstractController {

    /**
     * @desc 列表
    */
    public function listAction() {
        $request   = $this->getRequest();
        $class     = (int)$request->get('class');
        $status    = (int)$request->get('status');
        $keyword   = trim((string)$request->get('keyword'));
        $return    = [];

        if(!empty($keyword) && !empty($class)) {

            switch($class) {
                case 1  :
                    $first = ['uid' => $keyword];
                    break;
                case 2  :
                    $first = ['cid' => $keyword];
                    break;
                default :
                    $first = [];
                    break;
            }
        }

        if(!empty($status)) {
            $second = ['status' => $status];
        }

        $third = ['type' =>1 ];
        //组装查询语句
        if(!empty($first) && !empty($second)) {
            $where = array_merge($first, $second,$third);
        } elseif(!empty($first) || !empty($second)) {
            !empty($first) ? $where = array_merge($first,$third) : $where = array_merge($second,$third);
        }else{
            $where = $third;
        }

        $page = new \Lib\Paginator('inform');
        $page->where($where);
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models = $page->lists();
        $memberModel = MemberMapper::getInstance();
        $gameModel = GameCircleMapper::getInstance();


        foreach($models as $model) {
            $data = $model->toArray();
            $data['member_nick_name'] = urldecode($memberModel->findById($data['uid'])->getNick_name());
            $circleName = $gameModel->findById($data['cid']);

            if($circleName instanceof \Mapper\GameCircleModel) {
                $data['game_circle_name'] = $circleName->getName();
            }

            $return[] = $data;
        }

        $this->assign('lists',$return);

    }

    /**
     * @desc 详细
    */
    public function editAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $status = $request->get('status');
        $informMapper = \Mapper\InformModel::getInstance();

        if($request->isPost()) {
            try{
                $rows = $informMapper->update(['status'=>$status],['id'=>$id]);

                if($rows > 0){
                    return \Response::msg('处理成功！');
                }elseif($rows == 0) {
                    return \Response::msg('未改动！');
                }
            }catch (Exception $exc) {
                return \Response::errmsg($exc->getMessage());
            }

        }

        $data = $informMapper->findById($id)->toArray();
        $memberModel = MemberMapper::getInstance();
        $gameModel = GameCircleMapper::getInstance();
        $data['member_nick_name'] = urldecode($memberModel->findById($data['uid'])->getNick_name());
        $data['game_circle_name'] = $gameModel->findById($data['cid'])->getName();

        $this->assign('data',$data);
    }

}
